package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.PageResult;
import com.itheima.domain.PmsProduct;
import com.itheima.domain.PmsProductAttribute;
import com.itheima.domain.PmsProductCategory;
import com.itheima.mapper.PmsProductAttributeMapper;
import com.itheima.service.PmsProductAttributeService;
import com.itheima.service.PmsProductService;
import com.itheima.vo.ProductAttrInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品属性参数表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class PmsProductAttributeServiceImpl extends ServiceImpl<PmsProductAttributeMapper, PmsProductAttribute> implements PmsProductAttributeService {

    @Autowired
    PmsProductService pmsProductService;

    @Override
    public List<ProductAttrInfo> productAttrInfos(Integer productCategoryId) {
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(productCategoryId + ""), PmsProduct::getProductCategoryId, productCategoryId);
        List<PmsProduct> pmsProducts = pmsProductService.list(lqw);
        List<ProductAttrInfo>  productAttrInfos = new ArrayList<>();
        for (ProductAttrInfo productAttrInfo : productAttrInfos) {
            for (PmsProduct pmsProduct : pmsProducts) {
                Long productAttributeCategoryId = pmsProduct.getProductAttributeCategoryId();
                productAttrInfo.setAttributeCategoryId(productAttributeCategoryId);
                LambdaQueryWrapper<PmsProductAttribute> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(StrUtil.isNotBlank(productAttributeCategoryId + ""),PmsProductAttribute::getProductAttributeCategoryId,productAttributeCategoryId);
                List<PmsProductAttribute> pmsProductAttributes = list(lambdaQueryWrapper);
                for (PmsProductAttribute pmsProductAttribute : pmsProductAttributes) {
                    productAttrInfo.setAttributeId(pmsProductAttribute.getId());
                }
            }
        }
        return productAttrInfos;
    }

    @Override
    public PageResult listByCid(Integer cid, Integer type, Integer pageSize, Integer pageNum) {
        Page<PmsProductAttribute> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsProductAttribute> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(cid+""),PmsProductAttribute::getProductAttributeCategoryId,cid)
                .eq(StrUtil.isNotBlank(type+""),PmsProductAttribute::getType,type);
        page(page,lqw);
        return new PageResult(pageNum,pageSize,page.getTotal(),page.getPages(),page.getRecords());
    }
}
